package com.liu.network.netty;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * ChannelHandlerAdapter对网络事件进行写操作
 * 
 * @author ydxx
 *
 */
public class NettyClientHandler extends SimpleChannelInboundHandler<Object> {
	private byte[] firstMessage;
	String line = System.getProperty("line.separator");
	long stime = 0L;
	ByteBuf buf = null;

	/**
	 * 通道被添加 一个通道channel可以理解为一个客户端
	 */
	@Override
	public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
		System.out.println("handlerAdded");
	}

	/**
	 * 通道被注册
	 */
	public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
		ctx.fireChannelRegistered();
		System.out.println("channelRegistered");
	}

	/**
	 * 通道被激活 当客户端与服务器建立链路连接的时候 调用writeAndFlush发送请求数据到服务器端
	 */
	public void channelActive(ChannelHandlerContext ctx) {
		System.out.println("channelActive");
		stime = System.currentTimeMillis();
		try {
			firstMessage = tcp_ucp_flow(1, 2).toByteArray();
		} catch (IOException e) {
			e.printStackTrace();
		}
		buf = Unpooled.buffer(firstMessage.length);
		buf.writeBytes(firstMessage);
		System.out.println("数据包开始发送！！！ 数据大小：" + firstMessage.length);
		ctx.writeAndFlush(buf);
		System.out.println("客户端数据包发送完毕！！！ 发送时长：" + (System.currentTimeMillis() - stime) + "ms");
	}

	/**
	 * 通道开始读写
	 */
	protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
		System.out.println("channelRead0");
	}
	
	/**
	 * 通道开始读写
	 */
	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
		System.out.println("channelReadComplete");
	}

	/**
	 * 通道被闲置 当客户端与服务器建立链路连接的时候 调用writeAndFlush发送请求数据到服务器端
	 * 
	 */
	public void channelInactive(ChannelHandlerContext ctx) {
		System.out.println("channelInactive");
	}

	/**
	 * 通道被删除
	 */
	public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
		System.out.println("channelUnregistered");
		ctx.fireChannelRegistered();
	}

	/**
	 * 出现异常时关闭连接
	 */
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
		System.out.println("exceptionCaught");
		ctx.close();
	}

	public ByteArrayOutputStream tcp_ucp_flow(int submultiple, int pow) throws IOException {
		int max = (int) (submultiple * Math.pow(10, pow));
		System.out.println("数据总长度："+(2*max));
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		String key = null;
		String value = null;
		int leng;
		for (int i = 0; i < max; i++) {// udp:flow
			key = "udp:flow";
			value = "1560131603, 72, 0, 0, 0, 3232236156, NULL,1920103026,NULL, 64424, 53, 1, 0, 395, 0, 1, 0, 76, 0, 2, 0, 471, 0,0.500000, 0.500000, 0.838641, 0.161359, 0, 0, 21193,47.185394,47.185394, 18638.228516, 3586.089600, 1560131560010644,1560131560031837, 0, 4294967295, 8356419218836404200";
			 leng = key.length() + value.length();
			 out.write(intToBytes(leng));
			 out.write(intToBytes(key.length()));
			 out.write(intToBytes(value.length()));
			out.write(key.getBytes());
			out.write(value.getBytes());
		}

		for (int i = 0; i < max; i++) {// tcp:flow
			key = "tcp:flow";
			value = "1560131633, 72, 0, 0, 0, 3232236324, NULL,3072617490,NULL, 51729, 36688, 6, 759, 0, 0, 0, 5, 1, 0, 1, 1, 3, 1,453, 0,0, 0, 0, 0, 0, 0, 0, 1, 0, 1460, 2, 65536, 65516, 0, 399, 1,399,29227, 29158, 29290, 0.000000, 0.000000, 0.000000, 0.000000,0.000000, 0.000000, 0.600000, 0.744848, 4.610111, 583.179077,1013403942, 4, 260, 0, 0, 0, 4, 1, 0, 1, 1, 1, 1, 74, 0, 0,0, 0,0, 0, 0, 0, 1, 0, 1360, 7, 14720, 13600, 0, 20, 1, 20, 65190,223, 195020, 0.000000, 0.000000, 0.000000, 0.000000,0.000000,0.000000, 0.400000, 0.255152, 3.073408, 199.771500,212029613,223, 29290, 29513, 1301487, 1560131597002824,1560131598304311,0, 4294967295, 8677994484123678733";
			 leng = key.length() + value.length();
			 out.write(intToBytes(leng));
			 out.write(intToBytes(key.length()));
			 out.write(intToBytes(value.length()));
			out.write(key.getBytes());
			out.write(value.getBytes());
		}
//		for (int i = 0; i < max; i++) {// udp:flow
//			key = "udp:flow";
//			value = "1560131603, 72, 0, 0, 0, 3232236156, NULL,1920103026,NULL, 64424, 53, 1, 0, 395, 0, 1, 0, 76, 0, 2, 0, 471, 0,0.500000, 0.500000, 0.838641, 0.161359, 0, 0, 21193,47.185394,47.185394, 18638.228516, 3586.089600, 1560131560010644,1560131560031837, 0, 4294967295, 8356419218836404200";
//			// leng = key.length() + value.length();
//			// out.write(intToBytes(leng));
//			// out.write(intToBytes(key.length()));
//			// out.write(intToBytes(value.length()));
//			out.write(key.getBytes());
//			out.write(";".getBytes());
//			out.write(value.getBytes());
//			out.write(line.getBytes());
//		}
//
//		for (int i = 0; i < max; i++) {// tcp:flow
//			key = "tcp:flow";
//			value = "1560131633, 72, 0, 0, 0, 3232236324, NULL,3072617490,NULL, 51729, 36688, 6, 759, 0, 0, 0, 5, 1, 0, 1, 1, 3, 1,453, 0,0, 0, 0, 0, 0, 0, 0, 1, 0, 1460, 2, 65536, 65516, 0, 399, 1,399,29227, 29158, 29290, 0.000000, 0.000000, 0.000000, 0.000000,0.000000, 0.000000, 0.600000, 0.744848, 4.610111, 583.179077,1013403942, 4, 260, 0, 0, 0, 4, 1, 0, 1, 1, 1, 1, 74, 0, 0,0, 0,0, 0, 0, 0, 1, 0, 1360, 7, 14720, 13600, 0, 20, 1, 20, 65190,223, 195020, 0.000000, 0.000000, 0.000000, 0.000000,0.000000,0.000000, 0.400000, 0.255152, 3.073408, 199.771500,212029613,223, 29290, 29513, 1301487, 1560131597002824,1560131598304311,0, 4294967295, 8677994484123678733";
//			// leng = key.length() + value.length();
//			// out.write(intToBytes(leng));
//			// out.write(intToBytes(key.length()));
//			// out.write(intToBytes(value.length()));
//			out.write(key.getBytes());
//			out.write(";".getBytes());
//			out.write(value.getBytes());
//			out.write(line.getBytes());
//		}
//		out.write("end".getBytes());
//		out.write(line.getBytes());
		return out;
	}

	public ByteArrayOutputStream pstat(int submultiple, int pow) throws IOException {
		int max = (int) (submultiple * Math.pow(10, pow));
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		String key = null;
		String value = null;
		int leng;
		for (int i = 0; i < max; i++) {// p:stat
			key = "P:stat:link";
			value = "1560131573, 72, 255, 29753658, 17049880, 70746, 65429";
			leng = key.length() + value.length();
			out.write(intToBytes(leng));
			out.write(intToBytes(key.length()));
			out.write(intToBytes(value.length()));
			out.write(key.getBytes());
			out.write(value.getBytes());
			out.write(line.getBytes());
		}

		for (int i = 0; i < max; i++) {// p:stat
			key = "P:stat:network";
			value = "1560131573, 71, 253, 255, 0, 0, 0, 0";
			leng = key.length() + value.length();
			out.write(intToBytes(leng));
			out.write(intToBytes(key.length()));
			out.write(intToBytes(value.length()));
			out.write(key.getBytes());
			out.write(value.getBytes());
			out.write(line.getBytes());
		}

		for (int i = 0; i < max; i++) {// p:stat
			key = "P:stat:transport";
			value = "1560131573, 71, 2, 255, 0, 0, 0, 0";
			leng = key.length() + value.length();
			out.write(intToBytes(leng));
			out.write(intToBytes(key.length()));
			out.write(intToBytes(value.length()));
			out.write(key.getBytes());
			out.write(value.getBytes());
			out.write(line.getBytes());
		}

		for (int i = 0; i < max; i++) {// p:stat
			key = "P:stat:application";
			value = "1560131573, 72, 1, 1, 119, 0, 0, 0, 0, 141, 309,478";
			leng = key.length() + value.length();
			out.write(intToBytes(leng));
			out.write(intToBytes(key.length()));
			out.write(intToBytes(value.length()));
			out.write(key.getBytes());
			out.write(value.getBytes());
			out.write(line.getBytes());
		}
		return out;
	}

	public ByteArrayOutputStream pflow(int submultiple, int pow) throws IOException {
		int max = (int) (submultiple * Math.pow(10, pow));
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		String key = null;
		String value = null;
		int leng;
		for (int i = 0; i < max; i++) {// p:flow
			key = "P:flow:ipv4";
			value = "1560131453, 72, 3232236317, 2899908878, 57531, 443,6,1, 1, 66, 60, 20, 0, 11, 1, 1560131344843, 1560131442590,5912961334290265872, 0, 0, 962784134003, 176939654128322";
			leng = key.length() + value.length();
			out.write(intToBytes(leng));
			out.write(intToBytes(key.length()));
			out.write(intToBytes(value.length()));
			out.write(key.getBytes());
			out.write(value.getBytes());
			out.write(line.getBytes());
		}

		for (int i = 0; i < max; i++) {// p:flow
			key = "P:flow:totalv4";
			value = "1560131453, 72, 3232237410, 1931315921, 44836, 80,6, 5,5, 561, 571, 0, 0, 10, 1, 1560131394158, 1560131394311,6492812980453750594, 0, 1, 90520738408197, 17693965412";
			leng = key.length() + value.length();
			out.write(intToBytes(leng));
			out.write(intToBytes(key.length()));
			out.write(intToBytes(value.length()));
			out.write(key.getBytes());
			out.write(value.getBytes());
			out.write(line.getBytes());
		}

		for (int i = 0; i < max; i++) {// p:flow
			key = "P:flow:ipv6";
			value = "1560131453, 72, ff15::efc0:988f,fdf3:2f8a:9cfd:0:ecc7:dac5:3948:dc81, 6771, 6771, 17, 2, 0,402,0, 864, 462, 0, 1560131449234, 1560131451237,7203215139741218681, 0, 0, 56298158725263, 36232803451623";
			leng = key.length() + value.length();
			out.write(intToBytes(leng));
			out.write(intToBytes(key.length()));
			out.write(intToBytes(value.length()));
			out.write(key.getBytes());
			out.write(value.getBytes());
			out.write(line.getBytes());
		}

		for (int i = 0; i < max; i++) {// p:flow
			key = "P:flow:totalv6";
			value = "1560131453, 72, fe80::2cfb:825e:5c5f:881e,ff02::1:3,51172, 5355, 17, 0, 3, 0, 252, 658784, 211, 1, 1560131401251,1560131401664, 6584933363163707209, 0, 1, 75361035190029,56294136414211";
			leng = key.length() + value.length();
			out.write(intToBytes(leng));
			out.write(intToBytes(key.length()));
			out.write(intToBytes(value.length()));
			out.write(key.getBytes());
			out.write(value.getBytes());
			out.write(line.getBytes());
		}
		return out;
	}

	public static byte[] intToBytes(int value) {
		return new byte[] { (byte) ((value >> 24) & 0xFF), (byte) ((value >> 16) & 0xFF), (byte) ((value >> 8) & 0xFF),
				(byte) (value & 0xFF) };
	}

	@Override
	protected void messageReceived(ChannelHandlerContext ctx, Object msg) throws Exception {
		// TODO Auto-generated method stub
		
	}
}
